import * as bajaService from '@/services/bajaService';
import { MODULE_TYPE, TREEMENUITEM_TYPE } from '@/utils/constants';

export default {
  namespace: 'subModule',
  state: {
    $handle: '',
    $typeSpec: 'ibmsSupervisor:CommonModule',
    title: '',
    moduleType: 'Tree Menu',
    pagePath: '/subSystem',
    navOrd: '',
    treeMenus: [],
    selectedTreeMenu: null
  },
  reducers: {
    setData(state, { payload }) {
      return { ...state, ...payload };
    },
    setSelectedTreeMenu(state, {payload: {selectedTreeMenu}}) {
      return { ...state, selectedTreeMenu};
    }
  },
  effects: {
    *fetch({ payload: {subModuleOrd} }, { call, put }) {
      let $handle='', $typeSpec='', title='', moduleType='Tree Menu', pagePath='/subSystem', navOrd='', treeMenus=[];
      const subModule = yield call(bajaService.Ord, subModuleOrd);
      if(subModule) {
        $handle = subModule.$handle;
        $typeSpec = subModule.getType().$typeSpec;
        title = subModule.getTitle();
        const { $ordinal } =  subModule.getModuleType();
        moduleType = MODULE_TYPE[$ordinal];
        pagePath = subModule.getPagePath();
        navOrd = subModule.getNavOrd().toString();
        if(moduleType === 'TREE_MENU'||moduleType === 'HISTORY'||moduleType === 'ALARM'||moduleType === 'REALTIME'||moduleType === 'USER') {
          const treeMenuRoot = yield call(bajaService.Ord, navOrd+'/treeMenuRoot');
          if(treeMenuRoot) {
            treeMenus = yield call(bajaService.fetchNavChildren, treeMenuRoot);
            if(treeMenus) {
              for(let i=0; i<treeMenus.length; i++) {
                const { $typeSpec } = treeMenus[i].getType();
                if($typeSpec === "ibmsSupervisor:TreeMenuGroup") {
                  const treeMenuItems = yield call(bajaService.fetchNavChildren, treeMenus[i]);
                  treeMenus[i] = {
                    $handle: treeMenus[i].$handle,
                    $typeSpec: $typeSpec,
                    title: treeMenus[i].getTitle(),
                    icon: 'folder',
                    ord: treeMenus[i].getNavOrd().toString(),
                    key: treeMenus[i].$handle,
                  };
                  if(treeMenuItems) {
                    treeMenus[i].children = [];
                    for(let j=0; j<treeMenuItems.length; j++) {
                      const { $typeSpec } = treeMenuItems[j].getType();
                      if($typeSpec === "ibmsSupervisor:TreeMenuGroup") {
                        const itemTreeMenuItems = yield call(bajaService.fetchNavChildren, treeMenuItems[j]);
                        const itemGroup = {
                          $handle: treeMenuItems[j].$handle,
                          $typeSpec: $typeSpec,
                          title: treeMenuItems[j].getTitle(),
                          icon: 'folder',
                          ord: treeMenuItems[j].getNavOrd().toString(),
                          key: treeMenuItems[j].$handle,
                        };
                        itemGroup.children = itemTreeMenuItems.map(subItem => {
                          const { $ordinal } =  subItem.getTreeMenuItemType();
                          return ({
                            $handle: subItem.$handle,
                            $typeSpec: subItem.getType().$typeSpec,
                            title: subItem.getTitle(),
                            icon: subItem.getNIcon(),
                            url: subItem.getUrl(),
                            pagePath: subItem.getPagePath(),
                            ord: subItem.getNavOrd().toString(),
                            treeMenuItemType: TREEMENUITEM_TYPE[$ordinal],
                            key: subItem.$handle,
                          })
                        });
                        treeMenus[i].children.push(itemGroup);
                      } else if($typeSpec.endsWith("TreeMenuItem")) {
                        const { $ordinal } =  treeMenuItems[j].getTreeMenuItemType();
                        treeMenus[i].children.push({
                          $handle: treeMenuItems[j].$handle,
                          $typeSpec: treeMenuItems[j].getType().$typeSpec,
                          title: treeMenuItems[j].getTitle(),
                          icon: treeMenuItems[j].getNIcon(),
                          url: treeMenuItems[j].getUrl(),
                          pagePath: treeMenuItems[j].getPagePath(),
                          ord: treeMenuItems[j].getNavOrd().toString(),
                          treeMenuItemType: TREEMENUITEM_TYPE[$ordinal],
                          key: treeMenuItems[j].$handle,
                        });
                      }
                    }
                  }
                } else if($typeSpec.endsWith("TreeMenuItem")) {
                  const { $ordinal } =  treeMenus[i].getTreeMenuItemType();
                  treeMenus[i] = {
                    $handle: treeMenus[i].$handle,
                    $typeSpec: $typeSpec,
                    title: treeMenus[i].getTitle(),
                    icon: treeMenus[i].getNIcon(),
                    url: treeMenus[i].getUrl(),
                    pagePath: treeMenus[i].getPagePath(),
                    ord: treeMenus[i].getNavOrd().toString(),
                    treeMenuItemType: TREEMENUITEM_TYPE[$ordinal],
                    key: treeMenus[i].$handle,
                  }
                }
              }
            } else {
              treeMenus = []
            }
          }
        }
      }
      yield put({ type: 'setData', payload: {
        $handle, $typeSpec, title, moduleType, pagePath, navOrd, treeMenus, selectedTreeMenu:null
      }});
    },
    *clearAll({ _ }, { put }) {
      yield put({ type: 'setData', payload: {
        $handle: '',
        $typeSpec: 'ibmsSupervisor:CommonModule',
        title: '',
        moduleType: 'Tree Menu',
        pagePath: '/subSystem',
        navOrd: '',
        treeMenus: [],
        selectedTreeMenu: null
      }});
    },
    *selectTreeMenu({ payload: {selectedTreeMenu} }, { put }) {
      yield put({ type: 'setSelectedTreeMenu', payload: {selectedTreeMenu}});
    }
  }
}
